from django.core.management.base import BaseCommand
from catalog.models import Author, Book, Language, Genre
import random

class Command(BaseCommand):
    help = 'Seed the database with demo authors and books.'

    def handle(self, *args, **kwargs):
        # 1. 语言
        languages = ['English', 'Chinese', 'French', 'German', 'Japanese']
        language_objs = {}
        for lang in languages:
            obj, _ = Language.objects.get_or_create(name=lang)
            language_objs[lang] = obj

        # 2. 体裁
        genres = ['Novel', 'Essay', 'Science Fiction', 'Philosophy', 'History', 'Biography', 'Poetry']
        genre_objs = {}
        for g in genres:
            obj, _ = Genre.objects.get_or_create(name=g)
            genre_objs[g] = obj

        # 3. 作者
        authors_data = [
            {'name': '史铁生'},
            {'name': 'J.K. Rowling'},
            {'name': 'George Orwell'},
            {'name': 'Jane Austen'},
            {'name': '鲁迅'},
            {'name': '村上春树'},
            {'name': 'Fyodor Dostoevsky'},
            {'name': 'Gabriel García Márquez'},
            {'name': 'Virginia Woolf'},
            {'name': 'Franz Kafka'},
        ]
        author_objs = []
        for a in authors_data:
            obj, _ = Author.objects.get_or_create(name=a['name'])
            author_objs.append(obj)

        # 4. 书籍
        books_data = [
            {'title': '我与地坛', 'author': '史铁生', 'summary': '一部关于生命、苦难与思考的散文集。', 'cover_url': '/static/images/wo_yu_ditan.svg', 'language': 'Chinese', 'genre': ['Essay']},
            {'title': '务虚笔记', 'author': '史铁生', 'summary': '哲思与人生的交织，虚实之间的笔记。', 'cover_url': '/static/images/wuxu_biji.svg', 'language': 'Chinese', 'genre': ['Novel']},
            {'title': "Harry Potter and the Philosopher's Stone", 'author': 'J.K. Rowling', 'summary': 'The first adventure of Harry Potter at Hogwarts.', 'cover_url': '/static/images/harry_potter.svg', 'language': 'English', 'genre': ['Novel', 'Fantasy']},
            {'title': '1984', 'author': 'George Orwell', 'summary': 'A dystopian novel about totalitarianism and surveillance.', 'cover_url': '/static/images/1984.svg', 'language': 'English', 'genre': ['Science Fiction']},
            {'title': 'Pride and Prejudice', 'author': 'Jane Austen', 'summary': 'A classic novel of manners and marriage.', 'cover_url': '/static/images/pride_and_prejudice.svg', 'language': 'English', 'genre': ['Novel']},
            {'title': '呐喊', 'author': '鲁迅', 'summary': '中国现代文学的奠基之作，收录多篇经典短篇小说。', 'cover_url': '', 'language': 'Chinese', 'genre': ['Novel', 'Essay']},
            {'title': '挪威的森林', 'author': '村上春树', 'summary': '青春、孤独与成长的故事。', 'cover_url': '', 'language': 'Japanese', 'genre': ['Novel']},
            {'title': '罪与罚', 'author': 'Fyodor Dostoevsky', 'summary': '一部关于罪恶与救赎的哲学小说。', 'cover_url': '', 'language': 'Russian', 'genre': ['Philosophy', 'Novel']},
            {'title': '百年孤独', 'author': 'Gabriel García Márquez', 'summary': '魔幻现实主义的巅峰之作。', 'cover_url': '', 'language': 'Spanish', 'genre': ['Novel']},
            {'title': '到灯塔去', 'author': 'Virginia Woolf', 'summary': '意识流文学的代表作。', 'cover_url': '', 'language': 'English', 'genre': ['Novel']},
            {'title': '变形记', 'author': 'Franz Kafka', 'summary': '一部荒诞与现实交织的中篇小说。', 'cover_url': '', 'language': 'German', 'genre': ['Novel']},
            # 更多书籍可自行扩展
        ]
        # 自动生成ISBN
        def random_isbn():
            return str(random.randint(1000000000000, 9999999999999))

        for b in books_data:
            author = Author.objects.get(name=b['author'])
            language = language_objs.get(b['language'], list(language_objs.values())[0])
            cover_url = b['cover_url'] if b['cover_url'] else '/static/images/default_book.svg'
            book, created = Book.objects.get_or_create(
                title=b['title'],
                defaults={
                    'author': author,
                    'summary': b['summary'],
                    'isbn': random_isbn(),
                    'language': language,
                    'cover_url': cover_url,
                }
            )
            # 体裁
            for g in b['genre']:
                if g in genre_objs:
                    book.genre.add(genre_objs[g])

        self.stdout.write(self.style.SUCCESS('Demo authors and books created successfully!')) 